#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    struct cmpare
    {
        bool operator() (pair<string, int> p1, pair<string, int> p2)
        {
            return p1.second > p2.second;
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> mymap;
        for (auto& i : words)
            mymap[i]++;

        vector<pair<string, int>> tmp;
        for (auto& e : mymap)
            tmp.push_back(e);

        stable_sort(tmp.begin(), tmp.end(), cmpare());

        vector<string> ret;
        for (int i = 0; i < k; i++)
            ret.push_back(tmp[i].first);

        return ret;
    }
};